/*
 *说明：工单表 首页脚本
 *
 *@author mlc 2013-04-07
*/

var indexForm;
var grid;

$(function(){

	indexForm = $('#indexForm');

	grid=$('#listTable').datagrid({
		nowrap: false,
		striped: true,
		fit: false,
		frozenColumns:[[
            {title:'选择',field:'ck',checkbox:true}
		]],
		columns:[[

            {title:'工单ID',field:'order_id',align:'center',width:80},

            {title:'申请人',field:'apply_by',align:'center',width:80},

            {title:'申请时间',field:'apply_at',align:'center',width:80},

            {title:'申请原因',field:'apply_reason',align:'center',width:80},

            {title:'工单类型',field:'order_type',align:'center',width:80},

            {title:'审批人',field:'approve_by',align:'center',width:80},

            {title:'审批时间',field:'approve_at',align:'center',width:80},

            {title:'审批意见',field:'approve_content',align:'center',width:80},

            {title:'工单状态',field:'order_status',align:'center',width:80},

            {title:'创建人',field:'create_by',align:'center',width:80},

            {title:'创建时间',field:'create_at',align:'center',width:80},

            {title:'更新人',field:'update_by',align:'center',width:80},

            {title:'更新时间',field:'update_at',align:'center',width:80},

			{title:'操作',field:'operation',align:'center',width:100,
	        	formatter:function(value,rec){
         		var s = '<a style="cursor:pointer; color:blue" onclick=\'showInfo("' + rec.key + '")\'>查看明细</a>';
	        	    return s;
		   		}
		    }
        ]],
		pagination:true,
		rownumbers:true,
		singleSelect: false,
		pageSize:10,
		pageList:[15,20,30]
	});

	queryOperation();

	$('#queryBtn').click(queryOperation);
	$('#clearBtn').click(clearOperation);
	$('#reloadBtn').click(refreshWin);
	$('#addBtn').click(addOperation);
	$('#editBtn').click(editOperation);
	$('#deleteBtn').click(deleteOperation);
});

//翻页时自动调用此函数
function onLoadPrev(msg){

	var queryParams=grid.datagrid('options').queryParams;
	queryParams['firstQuery']=false;
}

//查询操作
function queryOperation(){

	var params = indexForm.serializeObject();
	grid.datagrid("options").pageNumber = 1;
	grid.datagrid({
		url: 'P_WF_ORDER!showList.do',
		queryParams: jQuery.extend({}, params, {'firstQuery': true})
	});
}

//清空操作（重置）
function clearOperation() {

	indexForm.resetForm();
}

//查看明细
function showInfo(fid) {

	var href = 'P_WF_ORDER!showInfo.do?model.key=' + fid;
	openWin(href,'pop',editWindowWidth,editWindowHeight); 
}

//添加工单表
function addOperation(){  

	var href = 'P_WF_ORDER!showAdd.do';
	openWin(href,'pop');
}

//编辑工单表
function editOperation(){  

	var rows = grid.datagrid('getSelections');

	if (rows.length == 0) {
		showInfoWin('请您首先选中一条记录!');
		return;
	}  

	if (rows.length > 1) {
		showInfoWin('只能选择一条记录');
      	return; 
	}

	var href = 'P_WF_ORDER!showEdit.do?id=' + rows[0].key;
	openWin(href,'pop');
}   

//删除工单表
function deleteOperation() {

	var rows = grid.datagrid('getSelections');

	if (rows.length == 0) {
		showInfoWin('请您首先选中要删除的记录!');	
		return;
	}

	var ids = [];

	for (var i = 0; i < rows.length; i++) {
		ids.push(rows[i].key);
	}

	var params = $ .param({'id' : ids}, true);

	$ .messager.confirm('提示信息', '您确定要删除选定的' + rows.length + '条记录吗?', function(result){
		if(result){
			$ .ajax({
				url: 'P_WF_ORDER!doDelete.do',
				data: params,
				type: 'post',
				success: function(data){
				    showInfoWin('操作成功!');
					afterOK();
				}
			});
		}	
	});
}

//操作成功后刷新列表
function afterOK(){

	queryOperation();
}